// 页面判断
function loadPage() {
  let root = document.querySelector('.rootSet')
  let stu = document.querySelector('.stuSet')
  let loginPage = JSON.parse(localStorage.getItem('login'))
  let user = document.querySelector('.top-right i')
  let iframe = document.querySelector('iframe')
  let rootSet = document.querySelector('.icon-shitiguanli')
  let stuSet = document.querySelector('.icon-wode-wodekaoshi')
  if (loadPage == 1) {
    document.querySelector('.main-message').style.display = 'none'
  }
  for (type of loginPage) {
    if (type.userType === '管理员') {
      stu.style.display = 'none'
      iframe.src = 'questionManager.html'
      rootSet.classList.add('active')
      if (user.classList.contains('icon-xuesheng')) {
        user.classList.remove('icon-xuesheng')
        user.classList.add('icon-guanliyuan_jiaoseguanli')
        console.log('管理员')
      }
    } else if (type.userType === '学生') {
      root.style.display = 'none'
      iframe.src = 'myExam.html'
      stuSet.classList.add('active')
      if (user.classList.contains('icon-guanliyuan_jiaoseguanli')) {
        user.classList.remove('icon-guanliyuan_jiaoseguanli')
        user.classList.add('icon-xuesheng')
        console.log('学生')
      }
    }
    user.innerHTML = type.username
  }
}
loadPage()

// 点击退出
document.querySelector('.top-right .exit').addEventListener('click', function () {
  localStorage.getItem('login')
  arr = 1
  localStorage.setItem('login', JSON.stringify(arr))
  document.querySelector('.main-message').style.display = 'none'
  goLogin()
})
function goLogin() {
  window.location.href = 'login.html'
}

// 时间的显示
let time = setInterval(
  function () {
    let year = new Date()
    let h = year.getHours()
    let m = year.getMinutes()
    let s = year.getSeconds()
    if (h < 10) {
      h = `0${h}`
    }
    if (m < 10) {
      m = `0${m}`
    }
    if (s < 10) {
      s = `0${s}`
    }
    document.querySelector('.year').innerHTML = year.toLocaleDateString()
    document.querySelector('.day').innerHTML = `${h}:${m}:${s}`
  }
)

// 点击三横杠内容隐藏
document.querySelector('.rootSet .icon-icon_daohang_caidan').addEventListener('click', function () {
  that = this
  nrYc()
})
document.querySelector('.stuSet .icon-icon_daohang_caidan').addEventListener('click', function () {
  that = this
  nrYc()
})
// 内容隐藏函数
function nrYc() {
  let arrLi = document.querySelectorAll('li')
  let side = document.querySelector('.sidebar-box')
  let rightBox = document.querySelector('.right-box')
  let iframe = document.querySelector('iframe')
  if (that.classList.contains('op')) {
    side.style.width = '4vw'
    rightBox.style.width = '100vw'
    iframe.style.width = '90vw'
    for (let i = 0; i < arrLi.length; i++) {
      arrLi[i].style.width = '4vw'
    }
    that.classList.remove('op')
    that.classList.add('cl')
  } else {
    side.style.width = '15vw'
    rightBox.style.width = '88vw'
    iframe.style.width = '80vw'
    for (let i = 0; i < arrLi.length; i++) {
      arrLi[i].style.width = '15vw'
    }
    that.classList.remove('cl')
    that.classList.add('op')
  }
}

// 页面嵌套
document.querySelector('.rootSet').addEventListener('click', function (e) {
  let page = document.querySelector('.right-box iframe')
  if (e.target.tagName === 'LI') {
    if (e.target.dataset.id > 0) {
      switch (e.target.dataset.id) {
        case '1':
          page.src = 'questionManager.html'
          break;
        case '2':
          page.src = 'examManager.html'
          break;
        case '3':
          page.src = 'resultManager.html'
          break;
        case '4':
          page.src = 'userManager.html'
          break;
        case '5':
          page.src = 'config.html'
          break;
      }
    }
  }
}
)
document.querySelector('.stuSet').addEventListener('click', function (e) {
  let page = document.querySelector('.right-box iframe')
  if (e.target.dataset.id > 0) {
    switch (e.target.dataset.id) {
      case '1':
        page.src = 'myExam.html'
        break;
      case '2':
        page.src = 'myExamResult.html'
        break;
      case '3':
        page.src = 'configStu.html'
        break;
    }
  }
})


let rootLi = document.querySelectorAll('.rootSet ul li')
rootLi.forEach((item, i) => {
  if (i > 0) {
    item.addEventListener('click', function () {
      let activeLi = document.querySelector('.rootSet ul .active')
      activeLi.classList.remove('active')
      this.classList.add('active')
    })
  }

})

let stuLi = document.querySelectorAll('.stuSet ul li')
stuLi.forEach((item, i) => {
  if (i > 0) {
    item.addEventListener('click', function () {
      let activeLi = document.querySelector('.stuSet ul .active')
      activeLi.classList.remove('active')
      this.classList.add('active')
    })
  }

})

function colorSx() {
  let colors = localStorage.getItem('color')
  document.documentElement.style.setProperty('--themeColor', colors)
}
colorSx()
// let a = setInterval(colorSx, 500)
